package com.test;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * Created by 41579 on 2017/4/21.
 */
public class DelayRunnable implements Runnable, Delayed {
    private String name;
    private long time;
    private Date date;

    public DelayRunnable(String name, long time) {
        this.name = name;
        this.time = time;
        date = new Date(new Date().getTime() + time);
    }

    @Override
    public void run() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return date.getTime() - new Date().getTime();
    }

    @Override
    public int compareTo(Delayed o) {
        if (this.getTime() > ((DelayRunnable) o).getTime()) {
            return 1;
        } else if (this.getTime() == ((DelayRunnable) o).getTime()) {
            return 0;
        } else {
            return -1;
        }
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
